CMAKE_MINIMUM_REQUIRED(VERSION 2.8.4)




PROJECT(ooViewer)




SET(LOCAL_PROJECT_NAME ooViewer)



SET(OOVIEWER_SOVERSION 0)
SET(OOVIEWER_MAJOR_VERSION 0)
SET(OOVIEWER_MINOR_VERSION 2)
SET(OOVIEWER_PATCH_VERSION 0)
SET(OOVIEWER_VERSION ${OOVIEWER_MAJOR_VERSION}.${OOVIEWER_MINOR_VERSION}.${OOVIEWER_PATCH_VERSION})



IF(NOT WIN32)
    IF(CMAKE_SIZEOF_VOID_P MATCHES "8")
        SET(LIB_POSTFIX "64" CACHE STRING "suffix for 32/64 dir placement")
        MARK_AS_ADVANCED(LIB_POSTFIX)
    ENDIF()

    SET(PKGCONFIG_FILES
            ooViewer
    )

    FOREACH(PKGCONFIG_FILE ${PKGCONFIG_FILES})
        CONFIGURE_FILE(${PROJECT_SOURCE_DIR}/packaging/pkgconfig/${PKGCONFIG_FILE}.pc.in
        ${PROJECT_BINARY_DIR}/packaging/pkgconfig/${PKGCONFIG_FILE}.pc
        @ONLY
        )
    INSTALL(FILES ${PROJECT_BINARY_DIR}/packaging/pkgconfig/${PKGCONFIG_FILE}.pc DESTINATION lib${LIB_POSTFIX}/pkgconfig)
    ENDFOREACH(PKGCONFIG_FILE)
ENDIF(NOT WIN32)






CONFIGURE_FILE("${PROJECT_SOURCE_DIR}/src/ooViewer/Version.cpp.in" "${PROJECT_SOURCE_DIR}/src/ooViewer/Version.cpp")







OPTION(OOVIEWER_BUILD_PLATFORM_ANDROID "" OFF)
IF(OOVIEWER_BUILD_PLATFORM_ANDROID)

    SET(ANDROID TRUE)
    SET(ANDROID_PLATFORM 5)

    SET(OOVIEWER_ANDROID_TEMPLATES "${CMAKE_SOURCE_DIR}/PlatformSpecifics/Android")

ENDIF(OOVIEWER_BUILD_PLATFORM_ANDROID)









INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include)



IF(NOT CMAKE_BUILD_TYPE)
    SET(CMAKE_BUILD_TYPE "Release")
ENDIF(NOT CMAKE_BUILD_TYPE)






OPTION(OOVIEWER_BUILD_DEBUG "" OFF)

IF(OOVIEWER_BUILD_DEBUG)
    SET(CMAKE_BUILD_TYPE "Debug")
ENDIF(OOVIEWER_BUILD_DEBUG)



IF(CMAKE_BUILD_TYPE MATCHES "Debug")
    SET(OOVIEWER_BUILD_DEBUG ON)
ENDIF(CMAKE_BUILD_TYPE MATCHES "Debug")


MESSAGE(STATUS "ooViewer build: ${CMAKE_BUILD_TYPE}")









IF( WIN32 )
    ADD_DEFINITIONS(-W4 -DOSG_LIBRARY)
ELSE( WIN32 )
    ADD_DEFINITIONS(-Wall -DOSG_LIBRARY -fPIC)
ENDIF( WIN32 )




CONFIGURE_FILE("${PROJECT_SOURCE_DIR}/include/ooViewer/Config.in" "${PROJECT_SOURCE_DIR}/include/ooViewer/Config")





IF( CMAKE_SIZEOF_VOID_P EQUAL 8 )

    ADD_DEFINITIONS(-DIS_X86_64=1)

ELSE( CMAKE_SIZEOF_VOID_P EQUAL 8 )

    ADD_DEFINITIONS(-DIS_X86_64=0)

ENDIF( CMAKE_SIZEOF_VOID_P EQUAL 8 )






IF( WIN32 )
    SET(OOVIEWER_OSG_INCLUDE "$ENV{PROGRAMFILES}\\OpenSceneGraph\\include" CACHE PATH "OSG Headers")
    SET(OOVIEWER_OSGODE_INCLUDE "$ENV{PROGRAMFILES}\\OpenSceneGraph\\include" CACHE PATH "osgODE Headers")
    SET(OOVIEWER_OOGAME_INCLUDE "$ENV{PROGRAMFILES}\\OpenSceneGraph\\include" CACHE PATH "ooGame Headers")

ELSE( WIN32 )

    IF( NOT ANDROID )

        EXECUTE_PROCESS(COMMAND pkg-config openscenegraph-osg --cflags-only-I OUTPUT_VARIABLE tmp_var)
        STRING(REGEX REPLACE "\n" "" tmp_var "${tmp_var}")
        STRING(REGEX REPLACE "-I" "" tmp_var "${tmp_var}")

        SET(OOVIEWER_OSG_INCLUDE ${tmp_var} CACHE PATH "OSG Headers")



        EXECUTE_PROCESS(COMMAND pkg-config osgODE --cflags-only-I OUTPUT_VARIABLE tmp_var)
        STRING(REGEX REPLACE "\n" "" tmp_var "${tmp_var}")
        STRING(REGEX REPLACE "-I" "" tmp_var "${tmp_var}")

        SET(OOVIEWER_OSGODE_INCLUDE ${tmp_var} CACHE PATH "osgODE Headers")



        EXECUTE_PROCESS(COMMAND pkg-config ooGame --cflags-only-I OUTPUT_VARIABLE tmp_var)
        STRING(REGEX REPLACE "\n" "" tmp_var "${tmp_var}")
        STRING(REGEX REPLACE "-I" "" tmp_var "${tmp_var}")

        SET(OOVIEWER_OOGAME_INCLUDE ${tmp_var} CACHE PATH "ooGame Headers")

    ENDIF( NOT ANDROID )

ENDIF( WIN32 )


MESSAGE(STATUS "OSG include: ${OOVIEWER_OSG_INCLUDE}")
MESSAGE(STATUS "osgODE include: ${OOVIEWER_OSGODE_INCLUDE}")
MESSAGE(STATUS "ooGame include: ${OOVIEWER_OOGAME_INCLUDE}")





IF( WIN32 )
    SET(OOVIEWER_OSG_LIBRARIES "$ENV{PROGRAMFILES}\\OpenSceneGraph\\lib" CACHE PATH "OSG Libraries")
    SET(OOVIEWER_OSGODE_LIBRARIES "$ENV{PROGRAMFILES}\\OpenSceneGraph\\lib" CACHE PATH "osgODE Libraries")
    SET(OOVIEWER_OOGAME_LIBRARIES "$ENV{PROGRAMFILES}\\OpenSceneGraph\\lib" CACHE PATH "ooGame Libraries")

ELSE( WIN32 )

    IF( NOT ANDROID )

        EXECUTE_PROCESS(COMMAND pkg-config openscenegraph-osg --libs-only-L OUTPUT_VARIABLE tmp_var)
        STRING(REGEX REPLACE "\n" "" tmp_var "${tmp_var}")
        STRING(REGEX REPLACE "-L" "" tmp_var "${tmp_var}")

        SET(OOVIEWER_OSG_LIBRARIES ${tmp_var} CACHE PATH "OSG libraries")



        EXECUTE_PROCESS(COMMAND pkg-config osgODE --libs-only-L OUTPUT_VARIABLE tmp_var)
        STRING(REGEX REPLACE "\n" "" tmp_var "${tmp_var}")
        STRING(REGEX REPLACE "-L" "" tmp_var "${tmp_var}")

        SET(OOVIEWER_OSGODE_LIBRARIES ${tmp_var} CACHE PATH "osgODE libraries")



        EXECUTE_PROCESS(COMMAND pkg-config ooGame --libs-only-L OUTPUT_VARIABLE tmp_var)
        STRING(REGEX REPLACE "\n" "" tmp_var "${tmp_var}")
        STRING(REGEX REPLACE "-L" "" tmp_var "${tmp_var}")

        SET(OOVIEWER_OOGAME_LIBRARIES ${tmp_var} CACHE PATH "ooGame libraries")

    ENDIF( NOT ANDROID )

ENDIF( WIN32 )


MESSAGE(STATUS "OSG libraries: ${OOVIEWER_OSG_LIBRARIES}")
MESSAGE(STATUS "osgODE libraries: ${OOVIEWER_OSGODE_LIBRARIES}")
MESSAGE(STATUS "ooGame libraries: ${OOVIEWER_OOGAME_LIBRARIES}")





IF( WIN32 )
    SET(OOVIEWER_OSG_BINARIES "$ENV{PROGRAMFILES}\\OpenSceneGraph\\bin" CACHE PATH "OSG binaries")
    MESSAGE(STATUS "OSG binaries: ${OSG_BINARIES}")
ENDIF( WIN32 )




IF(NOT ANDROID)
    INCLUDE_DIRECTORIES(${OOVIEWER_OSG_INCLUDE})
    INCLUDE_DIRECTORIES(${OOVIEWER_OSGODE_INCLUDE})
    INCLUDE_DIRECTORIES(${OOVIEWER_OOGAME_INCLUDE})

    LINK_DIRECTORIES( ${OOVIEWER_OSG_LIBRARIES} )
    LINK_DIRECTORIES( ${OOVIEWER_OSGODE_LIBRARIES} )
    LINK_DIRECTORIES( ${OOVIEWER_OOGAME_LIBRARIES} )
ENDIF(NOT ANDROID)





CONFIGURE_FILE(
    "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules/cmake_uninstall.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
    IMMEDIATE @ONLY)
ADD_CUSTOM_TARGET(uninstall
    "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake")





SET(OOVIEWER_DOXYGEN_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/doc" CACHE PATH "Doxygen output directory")



SET(OOVIEWER_TEMP_DOXYGEN_INPUT "${PROJECT_BINARY_DIR}/doc/ooviewer_doxygen_input_files.txt")
MARK_AS_ADVANCED(OOVIEWER_TEMP_DOXYGEN_INPUT)

FILE(WRITE ${OOVIEWER_TEMP_DOXYGEN_INPUT} "${PROJECT_SOURCE_DIR}/README\n")


FIND_PACKAGE(Doxygen)

IF(DOXYGEN_FOUND)
    ADD_CUSTOM_TARGET(  doc
                        ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/doc/Doxyfile
                        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
                        COMMENT "Generating API documentation with Doxygen"
                        VERBATIM
    )
ENDIF(DOXYGEN_FOUND)




SET(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/CMakeModules;${CMAKE_MODULE_PATH}")

INCLUDE(ooViewerMacros)

ADD_SUBDIRECTORY(src)
ADD_SUBDIRECTORY(doc)


IF( NOT ANDROID )
    OPTION(OOVIEWER_BUILD_EXAMPLES "Build ooViewer examples" OFF)
    MESSAGE(STATUS "ooViewer examples: ${OOVIEWER_BUILD_EXAMPLES}")


    IF( OOVIEWER_BUILD_EXAMPLES )
        ADD_SUBDIRECTORY( examples )
    ENDIF( OOVIEWER_BUILD_EXAMPLES )
ENDIF( NOT ANDROID )



IF( NOT ANDROID )
    OPTION(OOVIEWER_BUILD__APPLICATIONS "Build ooViewer applications" ON)
    MESSAGE(STATUS "ooViewer applications: ${OOVIEWER_BUILD__APPLICATIONS}")


    IF( OOVIEWER_BUILD__APPLICATIONS )
        ADD_SUBDIRECTORY( applications )
    ENDIF( OOVIEWER_BUILD__APPLICATIONS )
ENDIF( NOT ANDROID )




FILE(REMOVE ${OOVIEWER_TEMP_DOXYGEN_INPUT})




MESSAGE(STATUS "${PROJECT_NAME} install prefix: ${CMAKE_INSTALL_PREFIX}")





IF(ANDROID)
    SET(OOVIEWER_SOURCE_DIR "${PROJECT_SOURCE_DIR}")

    CONFIGURE_FILE("${OOVIEWER_ANDROID_TEMPLATES}/Android.mk.master.in" "${CMAKE_BINARY_DIR}/Android.mk")
    CONFIGURE_FILE("${OOVIEWER_ANDROID_TEMPLATES}/Application.mk.master.in" "${CMAKE_BINARY_DIR}/Application.mk")
    CONFIGURE_FILE("${OOVIEWER_ANDROID_TEMPLATES}/AndroidManifest.xml.master.in" "${CMAKE_BINARY_DIR}/AndroidManifest.xml")

    MESSAGE(STATUS "Android build: use <PATH_TO_NDK>/ndk-build NDK_APPLICATION_MK=Application.mk")
ENDIF(ANDROID)
